﻿using System;
using RootMotion;
using RootMotion.FinalIK;
using UnityEngine;

// Token: 0x02000321 RID: 801
public class PlayerIK : MonoBehaviour
{
	// Token: 0x0600149A RID: 5274 RVA: 0x00087B14 File Offset: 0x00085D14
	private void Start()
	{
		this.playercontrol = base.GetComponent<Player>();
		if (this.playercontrol == null)
		{
			this.isRealPlayer = false;
			this.backupPlayerControl = base.GetComponent<MicroControl>();
		}
		BipedReferences bipedReferences = new BipedReferences();
		BipedReferences.AutoDetectReferences(ref bipedReferences, base.transform, BipedReferences.AutoDetectParams.Default);
		this.ik = base.gameObject.AddComponent<FullBodyBipedIK>();
		if (!bipedReferences.root)
		{
			return;
		}
		this.ik.SetReferences(bipedReferences, null);
		this.ik.solver.iterations = 0;
		this.ik.solver.SetLimbOrientations(BipedLimbOrientations.UMA);
		this.aimIk = base.gameObject.AddComponent<AimIK>();
		this.aimIk.solver.SetChain(this.ik.solver.spineMapping.spineBones, base.transform);
		this.aimIk.GetIKSolver().Initiate(base.transform);
		this.aimIk.GetIKSolver().SetIKPositionWeight(0.9f);
		UnityEngine.Object.Destroy(this.ik);
		this.ik = null;
		this.ikSetUp = false;
	}

	// Token: 0x0600149B RID: 5275 RVA: 0x00087C40 File Offset: 0x00085E40
	private void Update()
	{
		if (!this.ikSetUp)
		{
			return;
		}
		float b = 0f;
		bool flag;
		if (this.isRealPlayer)
		{
			flag = (!this.playercontrol.IsFlying() && !this.playercontrol.isClimbing());
		}
		else
		{
			flag = (!this.backupPlayerControl.IsFlying() && !this.backupPlayerControl.isClimbing());
		}
		if (this.ikEnabled && !flag)
		{
			this.ikEnabled = false;
		}
		else if (!this.ikEnabled && flag)
		{
			this.ikEnabled = true;
		}
		if (this.ikEnabled)
		{
			bool flag2 = (!this.isRealPlayer) ? this.backupPlayerControl.isMoving : this.playercontrol.isMoving;
			if (flag2)
			{
				b = this.weightWhenWalking;
			}
			else
			{
				b = 1f;
			}
		}
		this.weight = Mathf.Lerp(this.weight, b, Time.deltaTime * 2f);
	}

	// Token: 0x0600149C RID: 5276 RVA: 0x00087D4F File Offset: 0x00085F4F
	public FullBodyBipedIK GetIk()
	{
		return this.ik;
	}

	// Token: 0x0600149D RID: 5277 RVA: 0x00087D57 File Offset: 0x00085F57
	public AimIK GetAimIk()
	{
		return this.aimIk;
	}

	// Token: 0x04001796 RID: 6038
	private FullBodyBipedIK ik;

	// Token: 0x04001797 RID: 6039
	private AimIK aimIk;

	// Token: 0x04001798 RID: 6040
	private Player playercontrol;

	// Token: 0x04001799 RID: 6041
	private MicroControl backupPlayerControl;

	// Token: 0x0400179A RID: 6042
	private bool ikEnabled = true;

	// Token: 0x0400179B RID: 6043
	private bool isRealPlayer = true;

	// Token: 0x0400179C RID: 6044
	private float weight;

	// Token: 0x0400179D RID: 6045
	public float weightWhenWalking = 0.3f;

	// Token: 0x0400179E RID: 6046
	private bool ikSetUp;
}
